<?php

namespace Home\Controller;

use Common\Controller\HomeBaseController;

class CartController extends HomeBaseController {
	function _initialize() {
		parent::_initialize ();
		

		 $this->assign ( "nav", "cart" );
		
	}

	

	function index (){


		$cartlist =D("Product")->get_sum();





		$this->assign ( "cartlist", $cartlist );



		$this->assign ( "nav", "cart" );
		
		
		$this->display ( "index" );
		
	}

//
//	public  function cart_edit(){
//
//
//		foreach(I('post.product') as $k=> $v){
//
//			$cart_list [$k] = array (
//
//				"id" => $k,
//				"num" => $v
//			)
//			;
//
//
//		}
//
//		cookie ( "product_cart", serialize ( $cart_list ) ,86400);
//
//
//		$this->success("编辑完成");
//
//
//
//
//	}

	function jia(){

		$id = $_REQUEST["id"];

		if($id){
			$cart_list = unserialize ( cookie ( "product_cart" ) );


			$cart_list[$id]["num"]++;

			cookie ( "product_cart", serialize ( $cart_list ) ,86400);
			$this->success ( "成功！" );
		}
		else{
			$this->success ( "错误！" );
		}



	}


	function jian(){

		$id = $_REQUEST["id"];

		if($id){
			$cart_list = unserialize ( cookie ( "product_cart" ) );


			$cart_list[$id]["num"]--;

			if($cart_list[$id]["num"]==0){
				unset($cart_list[$id]);
			}

//			unset($cart_list[$id]);
//
			cookie ( "product_cart", serialize ( $cart_list ) ,86400);
			$this->success ( "成功！" );
		}
		else{
			$this->success ( "错误！" );
		}



	}



//删除购物车里的商品

	public function cart_dell() {

		$id = $_REQUEST["id"];

		if($id){
			$cart_list = unserialize ( cookie ( "product_cart" ) );

			unset($cart_list[$id]);

			cookie ( "product_cart", serialize ( $cart_list ) ,86400);
			$this->success ( "删除成功！" );
		}
		else{
			cookie ( "product_cart", null );
			$this->success ( "清空购物车成功！" );
		}

	}



}